var e = require("../@babel/runtime/helpers/interopRequireDefault").default;

Object.defineProperty(exports, "__esModule", {
    value: !0
}), exports.default = void 0;

var t = require("../@babel/runtime/helpers/classCallCheck"), o = require("../@babel/runtime/helpers/createClass"), n = require("../utils/util.js"), r = e(require("../utils/Tips")), a = e(require("../api/wxRequest")), i = require("../config/baseUrl.js"), u = getApp(), l = function() {
    function e() {
        t(this, e);
    }
    return o(e, null, [ {
        key: "request",
        value: function() {
            var e = this, t = arguments.length > 0 && void 0 !== arguments[0] ? arguments[0] : {}, o = t.method, l = void 0 === o ? "POST" : o, s = t.baseUrl, c = void 0 === s ? u.globalData.baseUrl : s, d = t.url, h = void 0 === d ? "" : d, f = t.data, g = void 0 === f ? {} : f, v = t.token, p = void 0 === v || v, m = t.contentType, k = void 0 === m ? "application/json" : m, w = t.header, b = void 0 === w ? {} : w, y = t.fromSuperMemberGold, E = void 0 !== y && y, T = t.showError, q = void 0 === T || T, x = function t() {
                return new Promise(function(o, s) {
                    var d = p ? e.createAuthHeader() : {};
                    wx.request({
                        url: "".concat(c).concat(h),
                        timeout: i.timeout,
                        method: l,
                        header: (0, n.merge)({
                            "content-type": k
                        }, b, d),
                        data: g,
                        success: function(n) {
                            if (200 !== n.statusCode) q && r.default.modal("当前参与人数过多，请稍后再试"), console.error("服务端请求错误", n), 
                            console.error("请求数据", g), e.handleHttpException(n), s(n); else {
                                var i = n.data;
                                "10004" !== i.code || E ? o(i) : a.default.wxLogin().then(function(t) {
                                    return e.login(t, u.globalData.token);
                                }).then(function(o) {
                                    return e.saveToken(o), t();
                                }).then(function(e) {
                                    o(e);
                                }).catch(function(e) {
                                    s(e);
                                });
                            }
                        },
                        fail: function(e) {
                            console.error("网络请求发起失败", e), q && r.default.modal("当前参与人数过多，请稍后再试"), s(e);
                        }
                    });
                });
            };
            return p && !u.globalData.token ? new Promise(function(t, o) {
                a.default.wxLogin().then(function(t) {
                    return e.login(t);
                }).then(function(t) {
                    return e.saveToken(t), x();
                }).then(function(e) {
                    t(e);
                }).catch(function(e) {
                    o(e);
                });
            }) : x();
        }
    }, {
        key: "saveToken",
        value: function(e) {
            u.globalData.token = e.data.mini_program_cookie, e.data.phone && (u.globalData.phone = e.data.phone), 
            e.data && e.data.miniopenid && wx.setStorageSync("openid", e.data.miniopenid), e.data && e.data.unionid && wx.setStorageSync("unionid", e.data.unionid);
        }
    }, {
        key: "getToken",
        value: function() {
            return u.globalData.token;
        }
    }, {
        key: "login",
        value: function(e, t) {
            var o = this, n = arguments.length > 2 && void 0 !== arguments[2] ? arguments[2] : {
                showCodeErrorMsg: !0
            };
            return new Promise(function(a, i) {
                var u = o.lastLoginTime, l = void 0 === u, s = Date.now();
                o.lastLoginTime = s;
                if (!l && s - u <= 1e4) {
                    var c = "tokenInverval" + Date.now();
                    o[c] = setInterval(function() {
                        var e = o.getToken();
                        e && e !== t && (clearInterval(o[c]), a({
                            data: {
                                mini_program_cookie: o.getToken()
                            }
                        }));
                    }, 300);
                } else o.get("/KCard/chg-assist/user/login", {
                    code: e
                }, {
                    token: !1,
                    showError: n.showCodeErrorMsg
                }).then(function(e) {
                    "0" !== e.code ? (n.showCodeErrorMsg && r.default.modal("当前参与人数过多，请稍后再试"), i(e)) : a(e);
                }).catch(function(e) {
                    i(e);
                });
            });
        }
    }, {
        key: "handleHttpException",
        value: function(e) {
            switch (e.statusCode) {
              case 500:
                this.handleHttp500Exception(e);
                break;

              default:
                console.info("其他错误", e);
            }
        }
    }, {
        key: "handleHttp500Exception",
        value: function(e) {
            console.error("500-服务器内部错误：".concat(e.data.message));
        }
    }, {
        key: "createAuthHeader",
        value: function() {
            var e = u.globalData.token, t = {};
            return e && (t.Cookie = "mini_program_cookie=".concat(e)), t;
        }
    }, {
        key: "get",
        value: function(e, t, o) {
            return this.request((0, n.merge)({
                method: "GET",
                url: e,
                data: t
            }, o));
        }
    }, {
        key: "put",
        value: function(e, t, o) {
            return this.request((0, n.merge)({
                method: "PUT",
                url: e,
                data: t
            }, o));
        }
    }, {
        key: "post",
        value: function(e, t, o) {
            return this.request((0, n.merge)({
                method: "POST",
                url: e,
                data: t
            }, o));
        }
    }, {
        key: "delete",
        value: function(e, t, o) {
            return this.request((0, n.merge)({
                method: "DELETE",
                url: e,
                data: t
            }, o));
        }
    }, {
        key: "preLogin",
        value: function() {
            var e = this, t = arguments.length > 0 && void 0 !== arguments[0] ? arguments[0] : {
                showCodeErrorMsg: !1
            };
            return new Promise(function(o, n) {
                a.default.wxLogin().then(function(o) {
                    return e.login(o, u.globalData.token, {
                        showCodeErrorMsg: t.showCodeErrorMsg
                    });
                }).then(function(t) {
                    e.saveToken(t), o(t);
                }).catch(function(e) {
                    n(e);
                });
            });
        }
    } ]), e;
}();

exports.default = l;